package com.yanggu.mapreduce.keyvalueinputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2019/12/15 11:10
 * @description:
 */
public class KeyValueInputFormatDriver {

    public static void main(String[] args) throws Exception {
        args = new String[]{"input/KeyValueTextInputFormat", "output/KeyValueTextInputFormat"};

        //1. 获得job对象
        Configuration conf = new Configuration();
        //设置分隔符
        conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, " ");
        Job job = Job.getInstance(conf);

        //2. 设置关联mapper、reducer、driver
        job.setMapperClass(KeyValueInputFormatMapper.class);
        job.setReducerClass(KeyValueInputFormatReducer.class);
        job.setJarByClass(KeyValueInputFormatDriver.class);

        //3. 设置mapper的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //4. 设置最终的key的value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //5. 设置FileInputFormat的类型
        job.setInputFormatClass(KeyValueTextInputFormat.class);

        //6. 设置输入和输出的文件的路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //7. 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

}
